package com.zatom.newscore.config;

import com.zatom.newscore.utils.U;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
  @Value("${http.maxTotal:20}")
  Integer maxTotal;
  
  @Value("${http.connectTimeout:10000}")
  Integer connectTimeout;
  
  @Value("${http.readTimeout:15000}")
  Integer readTimeout;
  
  @Bean
  public RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial((KeyStore)null, acceptingTrustStrategy).build();
    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(1L, TimeUnit.HOURS);
    connectionManager.setMaxTotal(this.maxTotal.intValue());
    connectionManager.setDefaultMaxPerRoute(this.maxTotal.intValue());
    CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory((LayeredConnectionSocketFactory)csf).setConnectionManager((HttpClientConnectionManager)connectionManager).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient((HttpClient)httpClient);
    requestFactory.setConnectTimeout(this.connectTimeout.intValue());
    requestFactory.setReadTimeout(this.readTimeout.intValue());
    RestTemplate restTemplate = new RestTemplate((ClientHttpRequestFactory)requestFactory);
    Iterator<HttpMessageConverter<?>> var8 = restTemplate.getMessageConverters().iterator();
    while (var8.hasNext()) {
      HttpMessageConverter<?> c = var8.next();
      if (c instanceof MappingJackson2HttpMessageConverter) {
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.MULTIPART_FORM_DATA);
        mediaTypes.add(new MediaType("application", "*+json"));
        ((MappingJackson2HttpMessageConverter)c).setSupportedMediaTypes(mediaTypes);
        ((MappingJackson2HttpMessageConverter)c).setObjectMapper(U.objectMapper);
      } 
    } 
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    return restTemplate;
  }
}
